<?php

class Pages
{
	/**
	 *
	 * @var int|string Current page.
	 */
	public $currentPage;

	/**
	 *
	 * @var int Post per page. Default value 10.
	 */
	public $limitPerPage;

	/**
	 *
	 * @var string Link for url
	 */
	public $link;
        
	public $neighboursArrows;
        
        /**
	 *
	 * @var string Neighbours limit
	 */
	public $neighboursLimit;

	/**
	 *
	 * @var int Messages count.
	 */
	public $messageCount;

	/**
	 *
	 * @var string Class for current page.
	 */
	public $activeClass;

	public $startId;

	public $pageCount;

	public function __construct($page)
	{
		$this->currentPage = (Validate::integer($page, 1)) ? $page : 1;
		$this->messageCount = 0;
                $this->neighboursArrows = true;
                $this->neighboursLimit = 3;
		$this->limitPerPage = 1;
		$this->activeClass = '';
		$this->link = '';
		$this->startId = 0;
	}

	public function render()
	{
		if(!(is_numeric($this->currentPage) and $this->currentPage > 0)) {
			$this->limitPerPage = 0;
		}
		if(!$this->messageCount) {
			return false;
		}
		if($this->limitPerPage) {
			$this->startId		= (is_numeric($this->currentPage) and $this->currentPage > 1) ? ($this->currentPage-1)*$this->limitPerPage : 0;
			$this->pageCount	= ceil((int)$this->messageCount / (int)$this->limitPerPage);
			if($this->currentPage > $this->pageCount) {
				throw new ForbiddenException('Page doesn`t exist.');
			}
		} else {
			$this->startId	 = 0;
			$this->pageCount = 0;
		}

		return new View('page', array('link' => $this->link, 'count' => $this->pageCount, 'current' => $this->currentPage, 'neighboursLimit' => $this->neighboursLimit, 'neighboursArrows' => $this->neighboursArrows));
	}
}